/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.common.logger;

import org.telosys.common.TelosysException;

/**
 * ApplicationLogger wrapper to be secure when calling external log services<br>
 * ( just manage potential exceptions )
 * 
 * @author Laurent GUERIN
 *
 */
/* package */ class ApplicationLoggerFacade // implements IApplicationLogger
{

    private IApplicationLogger _appLogger = null ;
    
    //----------------------------------------------------------------------------------------
    /**
     * Constructs an ApplicationLogger wrapper
     * @param logger
     */
    protected ApplicationLoggerFacade(IApplicationLogger logger) throws TelosysException 
    {
        super();
        if ( logger != null )
        {
            _appLogger = logger;
        }
        else
        {
            throw new TelosysException ("Cannot create ApplicationLoggerFacade : logger param is null ");
        }
    }
    
    //----------------------------------------------------------------------------------------
    private void reportError(String s, Throwable ex)
    {
        // Risk of infinite loop => Do not re-call Telosys.xxx method ! 
        // => use only the Console log writer 
        Console.error("Exception in ApplicationLogger." + s + " : " + ex.getClass() + " - " + ex.getMessage() );
    }
    
    //----------------------------------------------------------------------------------------
    /**
     * Call the application logger "trace" method
     * @param sClassName
     * @param sMessage
     */
    protected void trace(String sClassName, String sMessage)
    {
        try
        {
            _appLogger.trace(sClassName, sMessage);
        } catch (Throwable e)
        {
            reportError("trace",e);
        }
    }

    //----------------------------------------------------------------------------------------
    /**
     * Call the application logger "debug" method
     * @param sClassName
     * @param sMessage
     */
    protected void debug(String sClassName, String sMessage)
    {
        try
        {
            _appLogger.debug(sClassName, sMessage);
        } catch (Throwable e)
        {
            reportError("debug",e);
        }
    }

    //----------------------------------------------------------------------------------------
    /**
     * Call the application logger "info" method
     * @param sClassName
     * @param sMessage
     */
    protected void info(String sClassName, String sMessage)
    {
        try
        {
            _appLogger.info(sClassName, sMessage);
        } catch (Throwable e)
        {
            reportError("info",e);
        }
    }

    //----------------------------------------------------------------------------------------
    /**
     * Call the application logger "warn" method
     * @param sClassName
     * @param sMessage
     */
    protected void warn(String sClassName, String sMessage)
    {
        try
        {
            _appLogger.warn(sClassName, sMessage);
        } catch (Throwable e)
        {
            reportError("warn",e);
        }
    }

    //----------------------------------------------------------------------------------------
    /**
     * Call the application logger "error" method
     * @param sClassName
     * @param sMessage
     */
    protected void error(String sClassName, String sMessage)
    {
        try
        {
            _appLogger.error(sClassName, sMessage);
        } catch (Throwable e)
        {
            reportError("error",e);
        }
    }

    //----------------------------------------------------------------------------------------
    /**
     * Call the application logger "error" method
     * @param sClassName
     * @param exception
     */
    protected void error(String sClassName, Throwable exception)
    {
        try
        {
            _appLogger.error(sClassName, exception);
        } catch (Throwable e)
        {
            reportError("error",e);
        }
    }
}
